package com.bobo.demo.Thread;

//模拟龟兔赛跑
public class Race implements Runnable {

    private static String winner;


    @Override
    public void run() {
        for (int i = 0; i < 200; i++) {
            boolean flag = GameOver(i);
            if(flag) {
                break;
            }
            System.out.println(Thread.currentThread().getName()+"跑了第 "+i+" 步");
        }
    }

    //判断是否完成了比赛
    private boolean GameOver(int step) {
        if(winner != null) {
            return true;
        } else {
            if(step >= 100) {
                winner = Thread.currentThread().getName();
                System.out.println(winner+"获得了比赛的胜利");
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Race t1 = new Race();
        //创建线程对象，通过线程对象来开启我们的现场，代理
        new Thread(t1,"乌龟").start();
        new Thread(t1,"兔子").start();
    }


}
